package com.tuo51.manager.controller;

import com.alibaba.fastjson.JSON;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import com.tuo51.manager.service.SysUserService;
import com.tuo51.manager.vo.SysUserVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/mapi/sysUser/")
@Api(value = "/mapi/sysUser/", tags = "OP - 系统用户管理")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @Autowired
    MemVerificationCache cache;


    @PostMapping("resetPassword")
    public StatusResponse resetPassword(@RequestHeader(TICKET) String ticket, String userId){
        String staffId = cache.getStaffId(ticket);
        return StatusResponse.success(sysUserService.resetPassword(userId));
    }

    /**
     * 修改密码
     */
    @PostMapping("updatePassword")
    public StatusResponse updatePassword(@RequestHeader(TICKET) String ticket, String oldPassword, String newPassword) {
        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(sysUserService.updatePassword(staffId, oldPassword, newPassword));
    }

    /**
     * 修改部分用户属性
     */
    @PostMapping("updateUser")
    public StatusResponse updateUser(@RequestHeader(TICKET) String ticket, String sysUserJSON) {
        String staffId = cache.getStaffId(ticket);

        SysUserVO sysUserVO = JSON.parseObject(sysUserJSON, SysUserVO.class);

        return StatusResponse.success(sysUserService.updateUser(sysUserVO));
    }


    @PostMapping("page")
    public StatusResponse page(@RequestHeader(TICKET) String ticket, PagerQuery pagerQuery) {
        String staffId = cache.getStaffId(ticket);
        return StatusResponse.success(sysUserService.page(pagerQuery));
    }

    @GetMapping("get/{id}")
    public StatusResponse get(@RequestHeader(TICKET) String ticket, @PathVariable("id") String id) {
        String staffId = cache.getStaffId(ticket);
        return  StatusResponse.success(SysUserVO.of(sysUserService.get(id)));
    }

    @PostMapping("save")
    public StatusResponse save(@RequestHeader(TICKET) String ticket, SysUserVO entityVo) {
        String staffId = cache.getStaffId(ticket);
        return  StatusResponse.success(sysUserService.save(entityVo));
    }

    @DeleteMapping("delete/{id}")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket,  @PathVariable("id") String id) {
        String staffId = cache.getStaffId(ticket);
        return  StatusResponse.success(sysUserService.delete(id));
    }
}
